package com.stellar.circle.basic.service.impl;

import com.stellar.circle.basic.dao.SPostLikeDao;
import com.stellar.circle.basic.entity.SPostLike;
import com.stellar.circle.basic.service.SPostLikeService;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帖子点赞表(SPostLike)服务实现
 *
 * @author Jqchan
 * @since 2025-05-19 14:23:03
 */
@Service
public class SPostLikeServiceImpl implements SPostLikeService {

    @Resource
    private SPostLikeDao sPostLikeDao;

    @Override
    public SPostLike queryById(Long id) {
        return sPostLikeDao.queryById(id);
    }

    @Override
    public SPostLike queryByCondition(SPostLike condition) {
        return sPostLikeDao.queryByCondition(condition);
    }

    @Override
    public List<SPostLike> queryAllByCondition(SPostLike condition) {
        return sPostLikeDao.queryAllByCondition(condition);
    }

    @Override
    public List<SPostLike> page(SPostLike condition, int page, int pageSize) {
        return sPostLikeDao.page(condition, page, pageSize);
    }

    @Override
    public int insert(SPostLike entity) {
        return sPostLikeDao.insert(entity);
    }

    @Override
    public int update(SPostLike entity) {
        return sPostLikeDao.update(entity);
    }

    @Override
    public int deleteById(Long id) {
        return sPostLikeDao.deleteById(id);
    }

    @Override
    public Long count(SPostLike condition) {
        return sPostLikeDao.count(condition);
    }

    @Override
    public Map<Long, Boolean> batchQueryLikeStatus(List<Long> postIds, Long userId, String type) {
        if (postIds == null || postIds.isEmpty() || userId == null) {
            return Collections.emptyMap();
        }

        // 查询存在的点赞记录
        List<SPostLike> likes = sPostLikeDao.batchQueryByPostIdsAndUser(
                postIds,
                userId,
                type,
                IsDeletedFlagEnum.UN_DELETED.getVal()
        );

        // 构建映射关系：postId -> 是否存在点赞
        Map<Long, Boolean> resultMap = new HashMap<>();
        for (Long postId : postIds) {
            boolean liked = false;
            for (SPostLike like : likes) {
                if (postId.equals(like.getPostId())) {
                    liked = true;
                    break;
                }
            }
            resultMap.put(postId, liked);
        }

        return resultMap;
    }
}
